package com.pd.controller.admin;

import com.pd.business.AccountBookTypeService;
import com.pd.business.UserService;
import com.pd.kit.JsonKit;
import com.pd.kit.LogUtil;
import com.pd.pojo.ResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by dong peng
 * DATE : 2017/2/23
 * TIME : 21:41.
 */

@Controller
public class ManageBaseController {

    @Autowired
    @Qualifier("userService")
    protected UserService userService;

    @Autowired
    @Qualifier("accountBookTypeService")
    protected AccountBookTypeService accountBookTypeService;

    /**
     * 请求参数中必须包含三个key，module,method,data
     * @param req
     * @param resp
     */
//    @RequestMapping("/ajax.do")
//    public void execute(HttpServletRequest req, HttpServletResponse resp) {
//        String module = req.getParameter("module");
//        String function = req.getParameter("method");
//
//
//        if(module != null) {
//            String instance = "com.pd.business."+module;
//            Object result = null;
//            try {
//                Class clazz = Class.forName(instance);
//                Method method = clazz.getMethod(function,HttpServletRequest.class,HttpServletResponse.class);
//                result = method.invoke(clazz.newInstance(),req,resp);
//
//                System.out.println(result.toString());
//            }catch (IllegalAccessException e) {
//                e.printStackTrace();
//            } catch (InvocationTargetException e) {
//                e.printStackTrace();
//            } catch (InstantiationException e) {
//                e.printStackTrace();
//            }catch (NoSuchMethodException e) {
//                e.printStackTrace();
//            } catch (ClassNotFoundException e) {
//                e.printStackTrace();
//            }
//
//            sendJsonResult(resp,result);
//        }
//
//    }

    /**
     * 请求数据以键值对的形式将请求参数作为data的值
     *
     * 获取请求参数并转为map集合
     *
     * @param request
     * @return
     */
    protected Map<String,Object> getPara(HttpServletRequest request) {
        Map map = new HashMap();
        String data = request.getParameter("data");
        LogUtil.debug("请求数据报文： "+data);

        if(data != null && !"".equals(data)){
            try {
                map = JsonKit.parse(data,Map.class);
            }catch (Exception e) {
                LogUtil.error("解析请求数据： ",e);
                e.printStackTrace();
                return map;
            }
        }
        LogUtil.debug("解析后数据： "+map);
        return map;
    }

    protected void sendJsonResult(HttpServletResponse resp, Object obj) {
        resp.setContentType("application/json");
        resp.setCharacterEncoding("UTF-8");
        String jsonStr = JsonKit.toJson(obj);
        try {
            resp.getWriter().println(jsonStr);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
